package com.yunding.house.controller;/*
 * Created by zgh on 2019/8/16
 * */
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunding.house.mapper.AgentMapper;
import com.yunding.house.pojo.Agent;
import com.yunding.house.service.AgentService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@RestController
@RequestMapping("/agent")
public class AgentController {

    /**
     * CRUD
     */
    @Autowired
    private AgentMapper agentMapper;
    @Autowired
    private AgentService agentService;


    @RequestMapping("/list")
    @ResponseBody
    public ModelAndView list(Model model,@RequestParam(value="start",defaultValue = "0")Integer start) {
        start= start < 0 ? 0 :start;
        PageHelper.startPage(start,5);
        List<Agent> list=agentService.findAll();
        PageInfo<Agent> pagehelper= new PageInfo<>(list);
        System.out.println(pagehelper);
        model.addAttribute("pagehelper",pagehelper);
        model.addAttribute("agentlist", agentService.findAll());
        model.addAttribute("title", "查看中介信息");
        return new ModelAndView("agent/list", "agentmodel", model);
    }
   /* @RequestMapping("/like")
    @ResponseBody
    public  ModelAndView like(Agent agent,Model model){
       QueryWrapper<Agent> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(agent.getAgentCompanyname())) {
            queryWrapper.like("agent_corpname", agent.getAgentCompanyname());
        }
        List<Agent> agentList = agentMapper.selectList(queryWrapper);
        agentList.forEach(System.out::println);
        model.addAttribute("agentLists",agentList);
        return new ModelAndView("agent/like", "agentmodel", model);
    }*/
   @RequestMapping("/like")
   @ResponseBody
    public  ModelAndView like(String agentname,Model model){
        QueryWrapper<Agent> queryWrapper = new QueryWrapper<>();
        if (null != agentname && !"".equals( agentname.trim())) {
            queryWrapper.like("agent_corpname", agentname);
        }
        List<Agent> agentList = agentMapper.selectList(queryWrapper);
        agentList.forEach(System.out::println);
       model.addAttribute("agentList",agentList);
       return new ModelAndView("agent/list", "agentmodel", model);
    }


    @RequestMapping("/selectone")
    @ResponseBody
    public ModelAndView selectone(Model model, int page, int size, Agent agent) {
        model.addAttribute("agent", agentService.search(5, 4, agent));
        return new ModelAndView("agen/view", "agentmodel", model);
    }

    @GetMapping("/form")
    @ResponseBody
    public ModelAndView createForm(Model model) {
        model.addAttribute("agent", new Agent());
        model.addAttribute("title", "云集家中介注入");
        return new ModelAndView("agent/register", "agentmodel", model);
    }

    @RequestMapping("/add")
    @ResponseBody
    public  ModelAndView  add(Agent agent){
        System.out.println("保存的员工信息"+agent);
        agentService.save(agent);
        return  new ModelAndView("redirect:/agent/list");
    }

    @RequestMapping("/modify/{id}")
    @ResponseBody
    public ModelAndView modify(@PathVariable("id") long id,Model model){
       Agent agent= agentService.selectone(id);
        model.addAttribute("title", "修改用户");
        model.addAttribute("agent",agent);
        return new ModelAndView("agent/form", "agentmodel", model);
    }
    @RequestMapping("/update")
    @ResponseBody
    public  ModelAndView  update(Agent agent){
        System.out.println("更改"+agent);
        agentService.update(agent);
        return  new ModelAndView("redirect:/agent/list");
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public  ModelAndView delete(@PathVariable("id") long id,Model model){
        agentService.del(id);
        return  new ModelAndView("redirect:/agent/list");
    }


}


